﻿using Tszy.Examples.Domain.Services.Users.Domain;
using Tszy.Examples.Domain.Services.Users.Infrastructure;
using Tszy.Unlimited.Data.UnitOfWorks.Defaults;

namespace Tszy.Examples.Domain.Features.Users;

internal class RegisterNewUser
{
    public static void Execute(Request request) => new Handler().Handle(request);

    public record Request(string Name);

    private class Handler
    {
        private readonly IUserRepository _userRepository = new UserRepository();
        public void Handle(Request request)
        {
            try
            {
                using var context = new DefaultUnitOfWork();

                var newUser = new DomainUser(request.Name);

                _userRepository.Add(newUser);

                context.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}